package model;

import java.util.LinkedList;
import java.util.Observable;

import util.StateCode;
import view.GridPanel;

public class ChessAraay extends Observable{
	public static final int ROWS=15;
	public static final int COLUMNS=15;
	private Chess[][] array=new Chess[ROWS][COLUMNS];
	private LinkedList<Chess> chessList=new LinkedList<>();
	private boolean isMy=false;
	public ChessAraay() {	
	}
	
	public boolean saveChess(Chess chess){
		int i=chess.getY(),j=chess.getX();
		if(array[i][j]==null) 
			{
				array[i][j]=chess;
				chessList.push(chess);
				setChanged();
				notifyObservers(StateCode.CHANGECHESSBOX);	
				isMy=isMy?false:true;
				if(hasFive(chess)){
					setChanged();
					notifyObservers(chess.getColor());
					isMy=false;
				}
				return true;
			}
		return false;
	}
	
	public void setChess(Chess chess){
		int i=chess.getY(),j=chess.getX();
		array[i][j]=chess;
	}
	
	public Chess getChess(int i,int j){
		return array[i][j];
	}
	
	public void clear(){
		for(int i=0;i<ROWS;i++)
			for(int j=0;j<COLUMNS;j++)
				array[i][j]=null;	
	}
	
	public void regret() {
		Chess chess;
		for (int a = 0; a < 2; a++) {
			chess = chessList.pop();
			int i = chess.getY();
			int j = chess.getX();
			array[i][j] = null;
		}
	}
	
	public boolean hasFive(Chess chess){
		int row=chess.getY();
		int col=chess.getX();
		int c=1;
		for(int j=col+1;j<COLUMNS&&array[row][j]!=null&&chess.equalColor(array[row][j]);j++) c++;
		for(int j=col-1;j>=0&&array[row][j]!=null&&chess.equalColor(array[row][j]);j--) c++;
		if(c>=5) return true;
		
		c=1;
		for(int j=row+1;j<COLUMNS&&array[j][col]!=null&&chess.equalColor(array[j][col]);j++) c++;
		for(int j=row-1;j>=0&&array[j][col]!=null&&chess.equalColor(array[j][col]);j--) c++;
		if(c>=5) return true;
		
		c=1;
		for(int i=row+1,j=col+1;j<COLUMNS&&array[i][j]!=null&&chess.equalColor(array[i][j]);i++,j++) c++;
		for(int i=row-1,j=col-1;j>=0&&array[i][j]!=null&&chess.equalColor(array[i][j]);i--,j--) c++;
		if(c>=5) return true;
		
		c=1;
		for(int i=row-1,j=col+1;j<COLUMNS&&array[i][j]!=null&&chess.equalColor(array[i][j]);i--,j++) c++;
		for(int i=row+1,j=col-1;j>=0&&array[i][j]!=null&&chess.equalColor(array[i][j]);i++,j--) c++;
		if(c>=5) return true;
		
		return false;
	}
	
	public LinkedList<Chess> getChessList(){
		return chessList;
	}
	
	public void replay(GridPanel gridPanel){		
		new Thread(new Runnable() {			
			@Override
			public void run() {
				clear();
				for(int i=chessList.size()-1;i>=0;i--){
					Chess chess=chessList.get(i);					
					try {
						Thread.sleep(1000);
					} catch (InterruptedException e) {
						// TODO Auto-generated catch block
						e.printStackTrace();
					}
					setChess(chess);
					gridPanel.repaint();
				}
				setChanged();
				notifyObservers(StateCode.REPLAYOVER);
			}
		}).start();
	}

	public boolean isMy() {
		return isMy;
	}

	public void setMy(boolean isMy) {
		this.isMy = isMy;
	}
	
}
